global input "Q:\dc1prhcmsas01\PU2\data - sas"
global temp "Q:\dc1prhcmsas01\PU2\temp_stata_wr"
global log "Q:\dc1prhcmsas01\PU2\Log - Stata WR"
global output "Q:\dc1prhcmsas01\PU2\data_stata_wr"
global graphs "Q:\dc1prhcmsas01\PU2\graphs_stata_wr"
global tables "Q:\dc1prhcmsas01\PU2\tables_stata_wr"

cd "Q:\dc1prhcmsas01\PU2\Code - Stata WR"
set scheme plotplain
graph set window fontface "Times New Roman"

capture log close
log using "$log/6_reg_wage_growth", replace

*Program to estimate regressions
cap program drop reg_est
program define reg_est 
args var name

	preserve 
	
	reghdfe `var' d_*, absorb(worker time) vce(cluster client_id)
	
	cap drop beta ub lb
	gen beta=.
	gen ub=.
	gen lb=.
	gen event_time=.
		
	local index=1
	forvalues i=-11/25 {
		replace event_time=`i'-8 if _n==`index'
		if `i'!=0 {
			replace beta=_b[d_`index'] if _n==`index'
			replace ub=_b[d_`index'] + 1.96*_se[d_`index'] if _n==`index'
			replace lb=_b[d_`index'] - 1.96*_se[d_`index'] if _n==`index'	
		}
		if `i'==0 {
			replace beta=0 if _n==`index'
			replace ub=0 if _n==`index'
			replace lb=0 if _n==`index'
		}
		local ++index
	}
	
	keep if beta!=.
	keep event_time beta ub lb	
	
	*Save estimates
	save "$output/`name'_estimates.dta", replace
	restore 
end 
	

*Program to estimate regressions
cap program drop reg_est_tab
program define reg_est_tab
args var name spec

	preserve 
	
	gen p1=time<=0 & treat==1
	gen p2=time>0 & time<=8 & treat==1
	gen p3=time>8 & treat==1
	gen slope1=time*p1
	gen slope2=time*p2 
	gen slope3=time*p3 
	
	*Variables to save estimates
		gen spec=""
		gen controls=""
		forvalues i=1/3 {
			gen beta`i'=.
			gen se`i'=.
		}
		gen n=.
		gen beta_change=.
		gen se_change=.
		
	*Regression
	reghdfe `var' p1 p2 p3 slope1 slope2 slope3, nocons absorb(worker time) vce(cluster client_id)

	replace spec="`spec'" if _n==1
	replace controls="Worker Time" if _n==1
	replace beta1=_b[slope1] if _n==1
	replace se1=_se[slope1] if _n==1
	replace beta2=_b[slope2] if _n==1
	replace se2=_se[slope2] if _n==1
	replace beta3=_b[slope3] if _n==1
	replace se3=_se[slope3] if _n==1
	replace n=`e(N)' if _n==1
	
	nlcom _b[slope3]-_b[slope1]
	replace beta_change=r(b)[1,1] if _n==1
	replace se_change=sqrt(r(V)[1,1]) if _n==1

	*Regression within firm
	reghdfe `var' p1 p2 p3 slope1 slope2 slope3, nocons absorb(worker client_id##time) vce(cluster client_id)

	replace spec="`spec'" if _n==2
	replace controls="Worker Firm-Time" if _n==2
	replace beta1=_b[slope1] if _n==2
	replace se1=_se[slope1] if _n==2
	replace beta2=_b[slope2] if _n==2
	replace se2=_se[slope2] if _n==2
	replace beta3=_b[slope3] if _n==2
	replace se3=_se[slope3] if _n==2
	replace n=`e(N)' if _n==2
	
	nlcom _b[slope3]-_b[slope1]
	replace beta_change=r(b)[1,1] if _n==2
	replace se_change=sqrt(r(V)[1,1]) if _n==2
	
	keep if _n<=2
	keep spec controls beta* se* n
	
	*Save estimates
	save "$output/`name'_table_estimates.dta", replace
	
	restore 
end 	

*********************************************************
*Fig 5a: Raw Averages
*********************************************************
	use yr_month client_id state emp_pur_c salaried base ot retire health base0 using "$output\stayers_panel_treat.dta", clear
	rename base0 base_apr
	
	*Merge on Dec 2016 base pay
	preserve 
	keep if yr_month==mdy(12,01,2016)
	rename base base0 
	rename salaried salaried0
	keep client_id state emp_pur_c base0 salaried0
	tempfile dec 
	save `dec'
	restore 
	
	merge m:1 client_id state emp_pur_c using `dec'
	drop _merge 
	
	*Keep main sample
	keep if base0>=913 & base0<993 & salaried0==1
	gen treat=base0>=913 & base0<953
	gen log_base=log(base)
	gen log_ot=log(ot)

	*Collapse and plot 
	preserve 
		collapse base, by(treat yr_month)
		gen time=mofd(yr_month)-mofd(mdy(4,01,2016))
		
		label define time_lab -12 "Apr 2015" -11 "May 2015" -10 "Jun 2015" ///
		-9 "Jul 2015" -8 "Aug 2015" -7 "Sep 2015" -6 "Oct 2015" -5 "Nov 2015" -4 "Dec 2015" ///
		-3 "Jan 2016" -2 "Feb 2016" -1 "Mar 2016" 0 "Apr 2016" 1 "May 2016" 2 "Jun 2016" ///
		3 "Jul 2016" 4 "Aug 2016" 5 "Sep 2016" 6 "Oct 2016" 7 "Nov 2016" 8 "Dec 2016" ///
		9 "Jan 2017" 10 "Feb 2017" 11 "Mar 2017" 12 "Apr 2017" 13 "May 2017" 14 "Jun 2017" ///
		15 "Jul 2017" 16 "Aug 2017" 17 "Sep 2017" 18 "Oct 2017" 19 "Nov 2017" 20 "Dec 2017" ///
		21 "Jan 2018" 22 "Feb 2018" 23 "Mar 2018" 24 "Apr 2018" 25 "May 2018", replace
		label values time time_lab 
		
		graph twoway (connect base time if treat==1, lc(black) mcolor(black)) ///
			(connect base time if treat==0, lc(maroon) mcolor(maroon) msymbol(D)), ///
			xline(1 8, lcolor(black)) xtitle("Month") xlabel(-12(3)25, valuelabel angle(45)) ///
			ytitle("Average Weekly Base Pay") ylabel(750(50)1050)  ///
			legend(subtitle("Base Pay in Dec 2016") order(1 "[913,953)" 2 "[953,993)") ring(0) position(5) col(2))
		graph export "$graphs/fig5a_base_pay_raw.eps", replace
		graph export "$graphs/fig5a_base_pay_raw.jpg", replace
	restore 

*********************************************************
*Fig 5b & Table 1 C1-C2 (Control is 953-993 in Dec 2016)
*********************************************************
	gen time=mofd(yr_month)-mofd(mdy(4,01,2016))
	local index=1
	forvalues i=-11/25 {
		gen d_`index'= treat==1 & time==`i'
		label var d_`index' "Treat=1 and Event Time=`i'"
		local ++index
	}
	drop d_12
	egen worker=group(emp_pur_c state client_id)
	
	reg_est base fig5b_base
	reg_est_tab base fig5b_base c953
	
	reg_est ot fig5b_ot
	reg_est_tab ot fig5b_ot c953
	
	reg_est log_base fig5b_log_base
	reg_est_tab log_base fig5b_log_base c953
	
	reg_est log_ot fig5b_log_ot
	reg_est_tab log_ot fig5b_log_ot c953
	
	reg_est retire fig5b_retire
	reg_est_tab retire fig5b_retire c953
	
	reg_est health fig5b_health
	reg_est_tab health fig5b_health c953
	
	drop worker 
	keep if treat==1 
	save "$temp/temp_treat2016.dta", replace
	
*********************************************************
*Fig A2 Placebo using 2014
*********************************************************
	use yr_month client_id state emp_pur_c salaried base ot retire health using "$output\stayers_panel_control.dta", clear
	
	*Merge on Dec 2014 base pay
	preserve 
	keep if yr_month==mdy(12,01,2014)
	rename base base0 
	rename salaried salaried0
	keep client_id state emp_pur_c base0 salaried0
	tempfile dec 
	save `dec'
	restore 
	
	merge m:1 client_id state emp_pur_c using `dec'
	drop _merge 
	
	*Keep main sample
	keep if base0>=913 & base0<993 & salaried0==1
	gen treat=base0>=913 & base0<953
	gen log_base=log(base)
	gen log_ot=log(ot)
	
	*Define regressors
	gen time=mofd(yr_month)-mofd(mdy(4,01,2014))
	local index=1
	forvalues i=-11/25 {
		gen d_`index'= treat==1 & time==`i'
		label var d_`index' "Treat=1 and Event Time=`i'"
		local ++index
	}
	drop d_12
	egen worker=group(emp_pur_c state client_id)
	
	*Regressions
	reg_est base figA2_base_placebo
	reg_est log_base figA2_log_base_placebo
	
*********************************************************
*Fig 5c & Table 1 C3-C4 (Control is 913-953 in Dec 2014)
*********************************************************
	drop worker
	*Define control group
	keep if treat==1
	replace treat=0
	forvalues i=1/37 {
		capture replace d_`i'=0
	}
	append using "$temp/temp_treat2016.dta"
	egen worker=group(emp_pur_c state client_id treat)
	
	reg_est base fig5c_base
	reg_est_tab base fig5c_base c953
	
	reg_est ot fig5c_ot
	reg_est_tab ot fig5c_ot c953
	
	reg_est log_base fig5c_log_base
	reg_est_tab log_base fig5c_log_base c953
	
	reg_est log_ot fig5c_log_ot
	reg_est_tab log_ot fig5c_log_ot c953
	
	reg_est retire fig5c_retire
	reg_est_tab retire fig5c_retire c953
	
	reg_est health fig5c_health
	reg_est_tab health fig5c_health c953
	drop worker
	
*********************************************************
*Fig 5d & Table 1 C5-C6
*********************************************************
	*Separate 2016 treatment into 2 groups
	keep if treat==1
	replace treat=0 if base_apr>=913
	forvalues i=1/37 {
		capture replace d_`i'=0 if treat==0
	}
	egen worker=group(emp_pur_c state client_id)
	
	reg_est base fig5d_base
	reg_est_tab base fig5d_base c953
	
	reg_est ot fig5d_ot
	reg_est_tab ot fig5d_ot c953
	
	reg_est log_base fig5d_log_base
	reg_est_tab log_base fig5d_log_base c953
	
	reg_est log_ot fig5d_log_ot
	reg_est_tab log_ot fig5d_log_ot c953
	
	reg_est retire fig5d_retire
	reg_est_tab retire fig5d_retire c953
	
	reg_est health fig5d_health
	reg_est_tab health fig5d_health c953
	
*********************************************************
*Fig A3
*********************************************************
*Program to estimate regressions
cap program drop reg_est
program define reg_est 
args var name

	preserve 
	
	reghdfe `var' d_*, nocons absorb(worker time treat#month) vce(cluster client_id)
	
	cap drop beta ub lb
	gen beta=.
	gen ub=.
	gen lb=.
	gen event_time=.
		
	local index=1
	forvalues i=-11/25 {
		replace event_time=`i'-8 if _n==`index'
		if `i'>0 {
			replace beta=_b[d_`index'] if _n==`index'
			replace ub=_b[d_`index'] + 1.96*_se[d_`index'] if _n==`index'
			replace lb=_b[d_`index'] - 1.96*_se[d_`index'] if _n==`index'	
		}
		if `i'<=0 {
			replace beta=0 if _n==`index'
			replace ub=0 if _n==`index'
			replace lb=0 if _n==`index'
		}
		local ++index
	}
	
	keep if beta!=.
	keep event_time beta ub lb	
	
	*Save estimates
	save "$output/`name'_estimates.dta", replace
	restore 
end 

gen month=month(yr_month)
drop d_1-d_11

	reg_est base figA3_base	
	reg_est ot figA3_ot	
	reg_est log_base figA3_log_base
	reg_est log_ot figA3_log_ot
	

log close
